<script setup>

import {ref} from "vue";
import dayjs from "dayjs";
import {baseUrl, reqInstance} from "@/utils/request/index.js";
import {message} from "ant-design-vue";

const visible = ref(false)

const emit = defineEmits(['create-completed'])

const defaultFormData = {
  id: 0,
  contract_id: "",
  project_type_setting_id: "",
  project_step: 0,
  estimated_money: 0,
  estimated_date: "",
  estimated_date_item: null,
}

const formData = ref(defaultFormData)

let contracts = [];


const openDraw = (myContracts,isEdit) => {
  visible.value = true
  getProjectTypes()
  console.log('myContracts',myContracts);
  for (const k in myContracts) {
    contracts.push({
      value: myContracts[k].id,
      label: myContracts[k].contract_no + " | " + myContracts[k].project_name
    })
  }
  if (!isEdit){
    formData.value = defaultFormData
  }
}
const proTypeFees = ref([])
const getProjectTypes = async () => {
  try {
    const res = await reqInstance({
      url: "/pro-type-fee/all",
      method: "get",
    })
    /**
     * {
     *     "id": 1,
     *     "type_title": "高企",
     *     "support_fixed_amount": true,
     *     "support_divide_amount": true,
     *     "created_at": "0001-01-01T00:00:00Z",
     *     "updated_at": "0001-01-01T00:00:00Z"
     * }
     */
    proTypeFees.value = (res.data?.data ?? []).map(({
                                                      id,
                                                      type_title,
                                                      support_fixed_amount,
                                                      support_divide_amount,
                                                    }) => {
      return {
        value: id,
        label: type_title,
        support_fixed_amount,
        support_divide_amount,
      }
    })
    console.log(proTypeFees);
  } catch (e) {

  }
}

const updateOpen = (myContracts,data) => {
  console.log('data',data);
  formData.value =  data
  formData.value.estimated_date_item =  dayjs.unix(data.estimated_date)
  openDraw(myContracts,true)
}

const doSave = async ()=>{
  let url = "receivable"
  let method = "post"
  if (formData.value.id > 0 ) {
    url += "/" + formData.value.id
    method = "put"
  }
  formData.value.estimated_money = parseFloat(formData.value.estimated_money)
  formData.value.estimated_date = formData.value.estimated_date_item.unix()
  try {
    await reqInstance({
      url: url,
      method: method,
      data: formData.value
    })
    message.success("保存成功")
    emit('create-completed')
    visible.value = false
  } catch (e) {

  }
}
defineExpose({openDraw,updateOpen})
</script>

<template>
  <a-drawer
      v-model:open="visible"
      class="custom-class"
      root-class-name="root-class-name"
      :root-style="{ color: 'blue' }"
      style="color: red"
      title="应收账款管理"
      placement="right"
  >
    <a-form layout="vertical" :model="formData">
      <a-form-item prop="com_name" label="关联合同">
        <a-select
            show-search v-model:value="formData.contract_id" :options="contracts"/>
      </a-form-item>
      <a-form-item prop="com_name" label="项目类型">
        <a-select
            show-search v-model:value="formData.project_type_setting_id" :options="proTypeFees"/>
      </a-form-item>
      <a-form-item label="项目阶段">
        <a-radio-group v-model:value="formData.project_step"  button-style="solid">
          <a-radio-button :value="0">进行中</a-radio-button>
          <a-radio-button :value="1">最终收款</a-radio-button>
        </a-radio-group>
      </a-form-item>
      <a-form-item prop="estimated_money" label="应收金额">
        <a-input v-model:value="formData.estimated_money" placeholder="请输入应收金额" />
      </a-form-item>
      <a-form-item label="预计收款日期">
        <a-date-picker v-model:value="formData.estimated_date_item" placeholder="请选择预计收款日期"/>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSave">保存</a-button>
      </a-form-item>
    </a-form>
  </a-drawer>
</template>

<style scoped>

</style>